import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_penting.dart';
import 'package:flutter_learn_vi/src/bean/bean_review.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

import 'PendindReviewPage.dart';
import 'PendingListItemWidget.dart';
import 'PendingReviewListItemWidget.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/25.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class PendingDetailsPage extends StatefulWidget {
  final PentingBean pentingBean;

  PendingDetailsPage({@required this.pentingBean});

  @override
  _PendingDetailsPageState createState() => _PendingDetailsPageState();
}

class _PendingDetailsPageState extends State<PendingDetailsPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    loading();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("活力职言"),
        centerTitle: true,
      ),
      backgroundColor: Colors.white,

      ///填充布局
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: RefreshIndicator(
          onRefresh: () {
            return getReview(isRefresh: true);
          },
          child: ListView.builder(
            itemCount: _list.length + 1,
            itemBuilder: (BuildContext context, int index) {
              if (index == 0) {
                return buildHeaderColumn();
              }
              ReviewBean reviewBean = _list[index - 1];
              return PendingReviewListItemWidget(
                index: index,
                pentingBean: widget.pentingBean,
                reviewBean: reviewBean,
              );
            },
          ),
        ),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.border_color),
        onPressed: () {
          if (widget.pentingBean.reviewId == null) {
            NavigatorUtils.pushPage(
                context,
                PendindReviewPage(
                  pentingBean: widget.pentingBean,
                ), callback: (value) {
              if (value != null) {
                widget.pentingBean.reviewId = 1;
                if (widget.pentingBean.reviewCount != null) {
                  widget.pentingBean.reviewCount++;
                }
                setState(() {});
              }
            });
          } else {
            ToastUtils.showToast("您已添加过回复");
          }
        },
      ),
    );
  }

  List<ReviewBean> _list = [];

  void loading() {
    getReview();
  }

  Future<bool> getReview({bool isRefresh = false}) async {
    Map<String, dynamic> map = {
      "pId": widget.pentingBean.id,
    };
    ResponseInfo responseInfo = await DioUtils.instance.getRequest(
      url: HttpHelper.pending_helper_review_list,
      queryParameters: map,
    );
    if (responseInfo.success) {
      _list = [];
      Map map = responseInfo.data;
      List list = map["list"];
      list.forEach((element) {
        ReviewBean newsBean = ReviewBean.fromMap(element);
        _list.add(newsBean);
      });
    }
    if (mounted) {
      setState(() {});
      if (isRefresh) {
        ToastUtils.showToast("已刷新");
      }
    }

    return Future.value(true);
  }

  Widget buildHeaderColumn() {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        PendingListItemWidget(
          pentingBean: widget.pentingBean,
          isShowCreateTime: true,
        ),
        Container(
          padding: EdgeInsets.only(left: 16, right: 16),
          child: Text(
            "全部评论",
            style: TextStyle(fontWeight: FontWeight.w500),
          ),
        ),
      ],
    );
  }
}
